mod check_func;
mod executor_func;
mod workflow_finish;

use check_func::action_check_example;
use executor_func::{get_comp_divi_emids, get_indi_divi_emids,get_hr_management_specialist,get_hr_supple_person};
use shq_common::prelude::SubmitType;
use workflow_finish::comp_divi_new_cb;

pub struct ExecutorFunc<'a> {
    name: &'a str,
}
impl<'a> ExecutorFunc<'a> {
    pub fn new(name: &'a str) -> Self {
        Self { name }
    }
    pub async fn exec(&self, run_id: u64, step_id: u8) -> anyhow::Result<Vec<u32>> {
        match self.name {
            "get_comp_divi_emids" => get_comp_divi_emids(run_id, step_id).await,
            "get_indi_divi_emids" => get_indi_divi_emids(run_id, step_id).await,
            "get_hr_management_specialist" => get_hr_management_specialist(run_id, step_id).await,
            "get_hr_supple_person" => get_hr_supple_person(run_id, step_id).await,
            _ => Err(anyhow::anyhow!("没有找到ExecutorFunc:{}", self.name)),
        }
    }
}

pub (crate) struct TaskCheckCallback<'a> {
    name: &'a str,
}

impl<'a> TaskCheckCallback<'a> {
    pub (crate) fn new(name: &'a str) -> Self {
        Self { name }
    }
    pub (crate) async fn check(
        &self,
        run_id: u64,
        step_id: u8,
        submit_type: SubmitType,
    ) -> anyhow::Result<bool> {
        match self.name {
            "action_check_example" => action_check_example(run_id, step_id, submit_type).await,
            _ => Err(anyhow::anyhow!("没有找到ActionCheckCallback:{}", self.name)),
        }
    }
}
pub (crate) struct WorkflowFinishCallBack<'a> {
    name: &'a str,
}
impl<'a> WorkflowFinishCallBack<'a> {
    pub (crate) fn new(name: &'a str) -> Self {
        Self { name}
    }
    pub (crate) async fn exec<'b>(&self, run_id: u64, user_id: u32,tx: &mut sqlx::Transaction<'b, sqlx::MySql>) -> anyhow::Result<()> {
        match self.name {
            "comp_divi_new_cb" => comp_divi_new_cb(tx, run_id, user_id).await,
            _ => Err(anyhow::anyhow!(
                "没有找到WorkflowFinishCallBack:{}",
                self.name
            )),
        }
    }
}
